package com.situ.springboot.controller;

import com.situ.springboot.pojo.Admin;
import com.situ.springboot.pojo.User;
import com.situ.springboot.pojo.query.AdminQuery;
import com.situ.springboot.service.IAdminService;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    //注入对象使用
    //@Autowired
    //private AdminMapper adminMapper;
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IUserService userService;

    // /admin/list
    @RequestMapping("/list1")
    @ResponseBody   //以json形式返回数据
    public List<Admin> list1() {
        System.out.println("AdminController.list1");
        List<Admin> list = adminService.list1();
        return list;
    }

    // /admin/list?page=1&limit=10&name=zhangsan&email=233@qq.com
    @RequestMapping("/list")
    @ResponseBody   //以json形式返回数据
    public PageResult list(AdminQuery adminQuery) {
        System.out.println("AdminController.list");
        PageResult pageResult = adminService.list(adminQuery);
        return pageResult;
    }

    //转发页面，返回String，不需要加@ResponseBody
    @RequestMapping("/toAdminList")
    public String toAdminList() {
        return "admin_list";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        adminService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        //Alt+Enter
        adminService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toAdminAdd")
    public String toAdminAdd() {
        return "admin_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Admin admin) {
        adminService.add(admin);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toAdminUpdate")
    public String toAdminUpdate() {
        return "admin_update";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Admin admin = adminService.selectById(id);
        return Result.ok(admin);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Admin admin) {
        adminService.update(admin);
        return Result.ok("修改成功");
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String name, String password, Integer type, String code, HttpSession session) {
        //1.先比较验证码是不是正确
        //2.如果验证码错误，直接返回：验证码错误
        //3.如果验证码正确，再比较用户名和密码
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        //Object 所有类的父类
        Object token = null;
        if (type == 1) {
            token = adminService.login(name, password);
        } else if (type == 2) {
            token = userService.login(name, password);
        }

        if (token == null) {
            return Result.error("用户名或密码错误");
        }

        //查看这个用户的status状态
        if (token instanceof Admin && ((Admin)token).getStatus() == 0) {
            return Result.error("该用户已经被禁用，请联系超级管理员");
        } else if (token instanceof User && ((User)token).getStatus() == 0) {
            return Result.error("该用户已经被禁用，请联系超级管理员");
        }

        //用户登录成功，把凭证放到session
        session.setAttribute("token", token);
        return Result.ok("登录成功", token);
    }

    @RequestMapping("/logout")
    public String logout( HttpSession session) {
        //移除凭证
        session.removeAttribute("token");
        //跳转到登录界面
        return "redirect:/admin/toLogin";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Integer id, Integer status) {
        adminService.updateStatus(id, status);
        return Result.ok("更新成功");
    }

    @RequestMapping("/toIndexAdminSuper")
    public String toIndexAdminSuper() {
        return "index_admin_super";
    }

    @RequestMapping("/toIndexAdminNormal")
    public String toIndexAdminNormal() {
        return "index_admin_normal";
    }

}
